//
//  ClubInfoTopCell.m
//  SportSocial
//
//  Created by yull on 15/1/10.
//  Copyright (c) 2015年 yull. All rights reserved.
//

#import "PlaceInfoTopCell.h"
#import <UIImageView+WebCache.h>
#import "StarView.h"

@implementation PlaceInfoTopCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        CGRect rect = CGRectMake(10, 10, 80, 80);
        self.iconImageView = [[UIImageView alloc] initWithFrame:rect];
        self.iconImageView.userInteractionEnabled = YES;
        [self.contentView addSubview:self.iconImageView];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [self.iconImageView addGestureRecognizer:tapGesture];
        
        self.photoNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.iconImageView.height - 16, self.iconImageView.width, 16)];
        self.photoNumberLabel.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        self.photoNumberLabel.textAlignment = NSTextAlignmentRight;
        self.photoNumberLabel.textColor = [UIColor whiteColor];
        self.photoNumberLabel.font = [UIFont systemFontOfSize:14];
        [self.iconImageView addSubview:self.photoNumberLabel];
        
        rect = CGRectMake(0, self.iconImageView.height - 15, self.iconImageView.width, 15.0f);
        self.iconCountLabel = [[UILabel alloc] initWithFrame:rect];
        self.iconCountLabel.backgroundColor = [UIColor clearColor];
        self.iconCountLabel.textAlignment = NSTextAlignmentRight;
        self.iconCountLabel.font = [UIFont systemFontOfSize:14.0f];
        self.iconCountLabel.textColor = [UIColor whiteColor];
        [self.iconImageView addSubview:self.iconCountLabel];
        
        CGFloat orginx = self.iconImageView.right + 10;
        rect = CGRectMake(orginx, 15.0f, self.contentView.width - orginx - self.iconImageView.left, 36);
        self.nameLabel = [[UILabel alloc] initWithFrame:rect];
        self.nameLabel.backgroundColor = [UIColor clearColor];
        self.nameLabel.textColor = [UIColor blackColor];
        self.nameLabel.font = [UIFont boldSystemFontOfSize:18.0f];
        self.nameLabel.numberOfLines = 2;
        self.nameLabel.adjustsFontSizeToFitWidth = YES;
        [self.contentView addSubview:self.nameLabel];
        
        self.starArray = [NSMutableArray array];
        
        self.separatorInset = UIEdgeInsetsMake(0, self.nameLabel.left, 0, 0);
    }
    return self;
}

+ (CGFloat)cellHeight
{
    return 90.0f;
}

- (void)drawCellWithPlace:(Place *)place
{
    self.nameLabel.text = place.name;

    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:place.icon] placeholderImage:[UIImage imageNamed:@"changguan"]];
    
    if (self.totalPhotoNumber) {
        self.photoNumberLabel.hidden = NO;
        self.photoNumberLabel.text = [NSString stringWithFormat:@"%ld张", (long)self.totalPhotoNumber];
    } else {
        self.photoNumberLabel.hidden = YES;
    }
    
    [self.starArray makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    CGFloat orginx = self.iconImageView.right + 10;
    CGFloat orginy = self.nameLabel.bottom + 10;
    for (NSInteger i = 0; i < place.rate; i++) {
        CGRect starRect = CGRectMake(orginx + i * 20 + 3*i, orginy, 20, 20);
        StarView *starImageView = [[StarView alloc] initWithFrame:starRect];
        starImageView.starColor = RGB(244, 100, 39);
        starImageView.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:starImageView];
        [self.starArray addObject:starImageView];
    }
}

- (UIEdgeInsets)layoutMargins
{
    return UIEdgeInsetsMake(0, self.nameLabel.left, 0, 0);
}

- (void)tapAction:(UITapGestureRecognizer *)tapGesture
{
    if ([self.delegate respondsToSelector:@selector(goToPlacePhotoAction)]) {
        [self.delegate goToPlacePhotoAction];
    }
}

@end
